<?

class Dir extends AbstractFile
{
    var $files_count = 0;
    var $directories_count = 0;
    var $sub_name;

    function Dir($name) {
        parent::AbstractFile($name);
        $dp = @opendir($name);
        while ($entry = @readdir($dp)) {
            if (is_dir($name.(substr($name,-1) == '/' ? '' : '/').$entry)) {
                if (($entry != '..') && ($entry != '.')) {
                    $this->directories_count++;
                }
            } else {
                $this->files_count++;
            }
        }
        $dirs = array_reverse(explode('/',$name));
        $this->sub_name = $dirs[0];
        $this->size = null;
    }

    function getSize() {
        if (is_null($this->size)) {
            $this->size = $this->_get_size($this->name);
        }
        return $this->size;
    }

    function getSizeF() {
        if (is_null($this->size)) {
            $this->size = $this->_get_size($this->name);
        }
        $size = $this->size;
        if ($size < 1024) {
            return $size.' b';
        } elseif ($size < 1024 * 1024) {
            return sprintf("%01.2f kb",$size/1024);
        } elseif ($size < 1024 * 1024 * 1024) {
            return sprintf("%01.2f Mb",$size/pow(1024,2));
        } else {
            return sprintf("%01.2f Gb",$size/pow(1024,3));
        }
    }

    function getFilesCount() {
        return $this->files_count;
    }

    function getDirectoriesCount() {
        return $this->directories_count;
    }

    function delete() {
        if (($this->getFilesCount() == 0) && ($this->getDirectoriesCount() == 0)) {
            rmdir($this->name);
        }
    }

//==============================================================================

    function _get_size($name) {
        if (substr($name,-1) != '/') {
            $name .= '/';
        }
        $size = 0;
        $dp = opendir($name);
        while ($entry = readdir($dp)) {
            if (is_file($name.$entry)) {
                $size += filesize($name.$entry);
            } elseif (($entry == '..') || ($entry == '.')) {
                continue;
            } else {
                $size += $this->_get_size($name.$entry);
            }
        }
        return $size;
    }

//    function


}

?>